package com.ygqh.baby.shiro.realm;

import com.ygqh.baby.po.YgAgentInviteRecord;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgAgentInviteRecordService;
import com.ygqh.baby.service.YgUserService;
import com.ygqh.baby.utils.ShareCodeUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 */
public class UserRealm extends AuthorizingRealm {
	
	@Autowired
    private YgUserService userService;
	@Autowired
    private YgAgentInviteRecordService ygAgentInviteRecordService;

    public void setUserService(YgUserService userService) {
        this.userService = userService;
    }
    
    /**
     * 授权信息
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = (String)principals.getPrimaryPrincipal();

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        authorizationInfo.setRoles(userService.findRoles(username));
//        authorizationInfo.setStringPermissions(userService.findPermissions(username));

        return null;
    }
    
    /**
     * 身份认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String username = (String)token.getPrincipal();

        YgUser user = userService.findByUserName(username);

        if(user == null) {
            throw new UnknownAccountException();//没找到帐号
        }

        user.setSourceCode(ShareCodeUtil.toSerialCode(user.getId()));
        YgAgentInviteRecord record = ygAgentInviteRecordService.findRecordForYijiandian(user.getId());
        if (record != null) {
            user.setFromCode(ShareCodeUtil.toSerialCode(record.getUserId()));
        } else {
            user.setFromCode("");
        }

        //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配，如果觉得人家的不好可以自定义实现
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user, //用户名
                user.getPassword(), //密码
                ByteSource.Util.bytes(user.getCredentialsSalt()),//salt=username+salt
                getName()  //realm name
        );
        return authenticationInfo;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }

}
